#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QTextCodec>//实现字符串转化的类
#include <stdio.h>


struct dict
{
    char *key;
    char *content;
};

int get_dict_size(FILE *pfile)//得到字典文件中词条总数
{
    if (pfile == NULL)
        return 0;

    int i = 0;
    char buf[2048];
    while (!feof(pfile))
    {
        fgets(buf, sizeof(buf), pfile);
        fgets(buf, sizeof(buf), pfile);
        i++;//读取两行后，计数器加1
    }
    return i;
}

//打开字典文件，并读取文件内容
int open_dict(struct dict **p, const char *dict_filename)
{
    FILE *pfile = fopen(dict_filename, "r");
    if (pfile == NULL)
        return 0;//打开文件失败，函数返回

    int size = get_dict_size(pfile);//得到字典文件中词条总数
    if (size == 0)
        return 0;

    *p = (struct dict *)malloc(sizeof(struct dict) * size);//根据字典文件词条总数分配内存
    memset(*p, 0, sizeof(struct dict) * size);//将分配内存初始化为0

    struct dict *pD = *p;//pD指向数组p的首地址

    char buf[2048] = { 0 };
    size_t len = 0;
    int i = 0;
    fseek(pfile, 0L, SEEK_SET);//设置读取位置为字典文件开始
    while (!feof(pfile))//循环读取文件，直到文件末尾
    {
        memset(buf, 0, sizeof(buf));
        fgets(buf, sizeof(buf), pfile);//读取文件一行
        len = strlen(buf);//得到读取到字符串长度
        if (len > 0)
        {
            pD[i].key = (char *)malloc(len);//根据字符串长度分配内存
            memset(pD[i].key, 0, len);
            strcpy(pD[i].key, &buf[1]);//将读取到的内容拷贝到key中
        }

        memset(buf, 0, sizeof(buf));
        fgets(buf, sizeof(buf), pfile);
        len = strlen(buf);
        if (len > 0)
        {
            pD[i].content = (char *)malloc(len);
            memset(pD[i].content, 0, len);
            strcpy(pD[i].content, &buf[6]);
        }
        i++;
    }
    fclose(pfile);//关闭字典文件

    return i;//返回读取到的字典词条数
}

//根据关键字key,在字典中查找内容
int search_dict(const struct dict *p, int size, const char *key, char *content)
{
    int i = 0;
    for (i = 0; i < size; i++)//遍历字典
    {
        if ((p[i].key == NULL) || (p[i].content == NULL))
            continue;

        if (strncmp(p[i].key, key, strlen(key)) == 0)
        {
            strcpy(content, p[i].content);
            return 1;//找到符合条件记录，返回1
        }
    }
    return 0;//没有找到符合条件记录，返回0
}

//释放内存
void free_dict(struct dict *p, int size)
{
    int i = 0;
    for (i = 0; i < size; i++)//循环释放key与content成员内存
    {
        if (p[i].key)
            free(p[i].key);
        if (p[i].content)
            free(p[i].content);
    }
    free(p);//释放p内存
}

struct dict *p = NULL;
int dict_size = 0;



Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    dict_size = open_dict(&p, "dict.txt");//根据命令行第一个参数做为字典文件名，打开字典文件
    if (dict_size == 0)
        exit(0);//打开字典文件失败，程序退出
}

Widget::~Widget()
{
    free_dict(p, dict_size);
    delete ui;
}

void Widget::on_pushButton_2_clicked()
{
    this->close();
}

void Widget::on_pushButton_clicked()
{
    char key[2048];
    char content[2048];
    memset(key, 0, sizeof(key));
    memset(content, 0, sizeof(content));


    QTextCodec *codec = QTextCodec::codecForName("GBK");//要实现GBK和utf8之间编码的转化

    //codec->fromUnicode(ui->lineEdit->text())

    //QString text1;
    //text1 = ui->lineEdit->text();//将用户在edit控件中输入的字符串放入text1


    strcpy(key, codec->fromUnicode(ui->lineEdit->text()));//把用户的输入转化为GBK编码，拷贝到key中

    if (search_dict(p, dict_size, key, content))//根据用户输入，在字典中检索
    {
        //codec->toUnicode(content);//将GBK编码格式转化为utf8格式
        ui->label->setText(codec->toUnicode(content));
        //QMessageBox::information(this, "a", content);

    } else
    {
        ui->label->setText("找不到");
        //QMessageBox::information(this, "a", "找不到");
    }
}
